//
//  AlertView.m
//  坠落的警告视图
//
//  Created by mac on 14-1-15.
//  Copyright (c) 2014年 mac. All rights reserved.
//

#import "AlertView.h"
#import "logInViewController.h"

#import "login_loginViewController.h"

#import "CALoginViewController.h"

typedef void(^UserAgreementBlock)();

@interface AlertView()

@property (nonatomic, copy) UserAgreementBlock userAgreementBlock;

@end
@implementation AlertView


- (id)initWithTitle:(NSString *)title
        contentText:(NSString *)content
        ButtonTitle:(NSString *)Title
      userAgreement:(void(^)())userAgreement{
    if (self = [super init]) {
        
        if (userAgreement != nil) {
            self.userAgreementBlock = userAgreement;
        }
        
        self.layer.cornerRadius = 5.0;
        //弹出框为白色底色
        self.backgroundColor = [UIColor whiteColor];
//        NSLog(@"高度：%f  提示高度：%f",kAlertHeight,(kAlertHeight)*0.1699);
        _alertTitleLabel =[[UILabel alloc]initWithFrame:CGRectMake(self.frame.origin.x, (kAlertHeight)*0.1699, MAIN_WIDTH-MAIN_WIDTH*0.09*2, 30)];
        _alertTitleLabel.textAlignment=NSTextAlignmentCenter;
        _alertTitleLabel.font=[UIFont fontWithName:@"Helvetica" size:16];
        _alertTitleLabel.text =@"欢迎加入";
//        [self addSubview:_alertTitleLabel];
        
        _logoImageView = [[UIImageView alloc] initWithFrame:(CGRectMake(0, 0,(MAIN_WIDTH - MAIN_WIDTH*0.09*2) * 0.4, (MAIN_WIDTH - MAIN_WIDTH*0.09*2)*0.4))];
        _logoImageView.image = [UIImage imageNamed:@"logoImage.png"];
        _logoImageView.center = _alertTitleLabel.center;
        [self addSubview:_logoImageView];
        
        
        
//     登录按钮
        _login_but = [[UIButton alloc]initWithFrame:CGRectMake(20, (kAlertHeight)*0.3495, MAIN_WIDTH-MAIN_WIDTH*0.09*2-40, 37)];
        [_login_but addTarget:self action:@selector(phone_login) forControlEvents:UIControlEventTouchUpInside];
        _login_but.backgroundColor =[UIColor colorWithRed:241/255.0 green:240 /255.0 blue:241/255.0 alpha:1];
//        _login_but.backgroundColor =[UIColor redColor];
        [_login_but setTitle:@"登录" forState:UIControlStateNormal];
        //给按钮上的文字修改颜色
        [_login_but setTitleColor:[UIColor colorWithRed:78/255.0 green:78/255.0 blue:78/255.0 alpha:1] forState:UIControlStateNormal];
        [self addSubview:_login_but];
        //按钮框
        CALayer *layer2 = _login_but.layer;
        layer2.borderColor = [UIColor colorWithRed:196/255.0 green:192/255.0 blue:203/255.0 alpha:1].CGColor;
        layer2.borderWidth = (2.5 / [UIScreen mainScreen].scale );
        _login_but.layer.masksToBounds = YES;
        _login_but.layer.cornerRadius =5;
        
//        注册按钮
        _register_but = [[UIButton alloc]initWithFrame:CGRectMake(20, (kAlertHeight )*0.46019+2, MAIN_WIDTH-MAIN_WIDTH*0.09*2-40, 37)];

        _register_but.backgroundColor =[UIColor colorWithRed:241/255.0 green:240 /255.0 blue:241/255.0 alpha:1];
        //        _login_but.backgroundColor =[UIColor redColor];
        [_register_but setTitle:@"注册" forState:UIControlStateNormal];
        //给按钮上的文字修改颜色
        [_register_but setTitleColor:[UIColor colorWithRed:78/255.0 green:78/255.0 blue:78/255.0 alpha:1] forState:UIControlStateNormal];
        [_register_but addTarget:self action:@selector(zhu_ce) forControlEvents:UIControlEventTouchUpInside];
        [self addSubview:_register_but];
        //按钮框
        CALayer *layer = _register_but.layer;
        layer.borderColor = [UIColor colorWithRed:196/255.0 green:192/255.0 blue:203/255.0 alpha:1].CGColor;
        layer.borderWidth = (2.5 / [UIScreen mainScreen].scale );
        _register_but.layer.masksToBounds = YES;
        _register_but.layer.cornerRadius =5;
        
        // 协议
        _agreement_label = [[UILabel alloc]initWithFrame:CGRectMake((kAlertWidth-160)/2+10, (kAlertHeight)*0.56+5, 160, 15)];
       
        _agreement_label.font=[UIFont fontWithName:@"Helvetica" size:12];
        _agreement_label.textAlignment=NSTextAlignmentCenter;
        NSMutableAttributedString *str = [[NSMutableAttributedString alloc] initWithString:[NSString stringWithFormat:@"我同意《雾霾以后用户协议》"]];
        [str addAttribute:NSForegroundColorAttributeName value:[UIColor redColor] range:NSMakeRange(3,10)];
        [_agreement_label setAttributedText:str];
        _agreement_label.userInteractionEnabled = YES;
        [_agreement_label addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(userAgreement)]];
        
        [self addSubview:_agreement_label ];
        
        //协议图标
        _agreement_image =[[UIImageView alloc]initWithFrame:CGRectMake(_agreement_label.frame.origin.x-12, (kAlertHeight)*0.56+6, 12, 12)];
        _agreement_image.image =[UIImage imageNamed:@"对勾框.png"];
        [self addSubview:_agreement_image];
        
        //两个线 于第三方登录
        _others_label =[[UILabel alloc]initWithFrame:CGRectMake((kAlertWidth-(kAlertWidth)*0.3129)/2, (kAlertHeight)*0.786, (kAlertWidth)*0.3129, 10)];
        _others_label.text =@"使用其他账号登录";
        //_others_label.font=[UIFont fontWithName:@"Helvetica" size:12];
        _others_label.font = [UIFont systemFontOfSize:12];
        _others_label.textAlignment=NSTextAlignmentCenter;
        _others_label.adjustsFontSizeToFitWidth=YES;
        [self addSubview:_others_label];
        
        _zuo = [[UIImageView alloc]initWithFrame:CGRectMake(20, (kAlertHeight)*0.786+4, (kAlertWidth)*0.258, 1)];
//        NSLog(@"宽度:%f",kAlertWidth-140-20*2-36*2);
        _zuo.backgroundColor =[UIColor redColor];
        [self addSubview:_zuo];
        
        _you =[[UIImageView alloc]initWithFrame:CGRectMake((kAlertWidth)-20-((kAlertWidth)*0.258), (kAlertHeight)*0.786+4, (kAlertWidth)*0.258, 1)];
        _you.backgroundColor =[UIColor redColor];
        [self addSubview:_you];
        
        _QQ = [[UIButton alloc]initWithFrame:CGRectMake(40, (kAlertHeight)*0.846, 43, 43)];
//        NSLog(@"相对高度：%f",kAlertHeight -105+26);
        [_QQ setBackgroundImage:[UIImage imageNamed:@"UMS_qq_icon"] forState:UIControlStateNormal];
        [_QQ addTarget:self action:@selector(qq_login) forControlEvents:UIControlEventTouchUpInside];
        [self addSubview:_QQ];
        
        _weixin =[[UIButton alloc]initWithFrame:CGRectMake((MAIN_WIDTH-MAIN_WIDTH*0.09*2)/2-43/2,  (kAlertHeight)*0.846, 43, 43)];
        [_weixin setBackgroundImage:[UIImage imageNamed:@"UMS_wechat_session_icon"] forState:UIControlStateNormal];
                [_weixin addTarget:self action:@selector(weixin_login) forControlEvents:UIControlEventTouchUpInside];
        [self addSubview:_weixin];
        
        _weibo =[[UIButton alloc]initWithFrame:CGRectMake((MAIN_WIDTH-MAIN_WIDTH*0.09*2)-40-43, (kAlertHeight)*0.846, 43, 43)];
        [_weibo addTarget:self action:@selector(weibo_login) forControlEvents:UIControlEventTouchUpInside];
        [_weibo setBackgroundImage:[UIImage imageNamed:@"UMS_sina_icon"] forState:UIControlStateNormal];
        [self addSubview:_weibo];

        
    }
     [self.Btn setBackgroundColor:[UIColor colorWithRed:227.0/255.0 green:100.0/255.0 blue:83.0/255.0 alpha:1]];
    [self.Btn setTitle:Title forState:UIControlStateNormal];
    self.Btn.titleLabel.font  = [UIFont boldSystemFontOfSize:14];
    [self.Btn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    
    [self.Btn addTarget:self action:@selector(BtnClicked:) forControlEvents:UIControlEventTouchUpInside];
    
    
    
    self.Btn.layer.cornerRadius = 3.0;
    [self addSubview:self.Btn];
    
    
//    self.alertTitleLabel.text = title;
//    self.alertContentLabel.text = content;
    

    
    
    UIButton *xButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [xButton setImage:[UIImage imageNamed:@"关闭.png"] forState:UIControlStateNormal];
    [xButton setImage:[UIImage imageNamed:@"关闭.png"] forState:UIControlStateHighlighted];
    xButton.frame = CGRectMake( MAIN_WIDTH-MAIN_WIDTH*0.09*2 - 32, 8, 25, 25);
    [self addSubview:xButton];
    [xButton addTarget:self action:@selector(dismissAlert) forControlEvents:UIControlEventTouchUpInside];
    return self;
}

- (void)userAgreement{
    
    if (self.userAgreementBlock != nil) {
        [self dismissAlert];
        self.userAgreementBlock();
    }
    
}

-(void)phone_login{
    
    [self dismissAlert];
//    login_loginViewController *login = [[login_loginViewController alloc]init];
//    login.hidesBottomBarWhenPushed = YES;
////    self.tabba.tabbarview.hidden =YES;
////    self.tabba.btn.hidden =YES;
//    _Controller.tabBarController.tabBar.hidden =YES;
//    [_Controller pushViewController:login animated:YES];
    
    if (_loginBlock != nil) {
        _loginBlock(nil,@"1");
    }  
    
//    NSLog(@"%@",_Controller.title);
}
-(void)zhu_ce{
    [self dismissAlert];
    
//    logInViewController *zhuCe = [[logInViewController alloc]init];
//    zhuCe.hidesBottomBarWhenPushed = YES;
////    self.tabba.tabbarview.hidden =YES;
////    self.tabba.btn.hidden =YES;
//    _Controller.tabBarController.tabBar.hidden =YES;
//    [_Controller pushViewController:zhuCe animated:YES];
    
    if(_registerBlock != nil){
        _registerBlock();
    }

}
-(void)qq_login{
    //QQ
    [self dismissAlert];
    
    UMSocialSnsPlatform *snsPlatform = [UMSocialSnsPlatformManager getSocialPlatformWithName:UMShareToQQ];
    
    snsPlatform.loginClickHandler(_Controller,[UMSocialControllerService defaultControllerService],YES,^(UMSocialResponseEntity *response){
        
        //          获取微博用户名、uid、token等
//        DLog(@"结果：%u",response.responseCode);
        if (response.responseCode == UMSResponseCodeSuccess) {
            
            UMSocialAccountEntity *snsAccount = [[UMSocialAccountManager socialAccountDictionary] valueForKey:UMShareToQQ];
            
            NSLog(@"username is %@, uid is %@, token is %@ url is %@",snsAccount.userName,snsAccount.usid,snsAccount.accessToken,snsAccount.iconURL); 
            
            if (_loginBlock != nil) {
                _loginBlock(snsAccount.usid,@"2");
            }
            
            
        }});

}
-(void)weibo_login{
    
    [self dismissAlert];
    UMSocialSnsPlatform *snsPlatform = [UMSocialSnsPlatformManager getSocialPlatformWithName:UMShareToSina];
    
    snsPlatform.loginClickHandler(_Controller,[UMSocialControllerService defaultControllerService],YES,^(UMSocialResponseEntity *response){
        
        //          获取微博用户名、uid、token等
        
        if (response.responseCode == UMSResponseCodeSuccess) {
            
            UMSocialAccountEntity *snsAccount = [[UMSocialAccountManager socialAccountDictionary] valueForKey:UMShareToSina];
            
            NSLog(@"username is %@, uid is %@, token is %@ url is %@",snsAccount.userName,snsAccount.usid,snsAccount.accessToken,snsAccount.iconURL);
            
            if (_loginBlock != nil) {
                _loginBlock(snsAccount.usid,@"3");
            }
            
        }});
}
-(void)weixin_login{
    
    [self dismissAlert];
    UMSocialSnsPlatform *snsPlatform = [UMSocialSnsPlatformManager getSocialPlatformWithName:UMShareToWechatSession];
    
    snsPlatform.loginClickHandler(_Controller,[UMSocialControllerService defaultControllerService],YES,^(UMSocialResponseEntity *response){
        
        if (response.responseCode == UMSResponseCodeSuccess) {
            
            NSDictionary *dict = [UMSocialAccountManager socialAccountDictionary];
            UMSocialAccountEntity *snsAccount = [[UMSocialAccountManager socialAccountDictionary] valueForKey:UMShareToWechatSession];
            
//            NSLog(@"\nusername = %@,\n usid = %@,\n token = %@ iconUrl = %@,\n unionId = %@,\n thirdPlatformUserProfile = %@,\n thirdPlatformResponse = %@ \n, message = %@",snsAccount.userName,snsAccount.usid,snsAccount.accessToken,snsAccount.iconURL, snsAccount.unionId, response.thirdPlatformUserProfile, response.thirdPlatformResponse, response.message);
//            
            NSLog(@"\nusername = %@,\n usid = %@,\n token = %@ iconUrl = %@,\n unionId = %@",snsAccount.userName,snsAccount.usid,snsAccount.accessToken,snsAccount.iconURL, snsAccount.unionId);
            
            if (_loginBlock != nil) {
                _loginBlock(snsAccount.usid,@"4");
            }
            
        }
        
    });
    
}
- (void)BtnClicked:(id)sender
{
    
    [self dismissAlert];
    
}
#pragma mark - 获取当前活动的viewcontroller
- (UIViewController*)topViewController {
    return [self topViewControllerWithRootViewController:[UIApplication sharedApplication].keyWindow.rootViewController];
}

- (UIViewController*)topViewControllerWithRootViewController:(UIViewController*)rootViewController {
    if ([rootViewController isKindOfClass:[UITabBarController class]]) {
        
        UITabBarController* tabBarController = (UITabBarController*)rootViewController;
        return [self topViewControllerWithRootViewController:tabBarController.selectedViewController];
    } else if ([rootViewController isKindOfClass:[UINavigationController class]]) {
        
        UINavigationController* navigationController = (UINavigationController*)rootViewController;
        return [self topViewControllerWithRootViewController:navigationController.visibleViewController];
    } else if (rootViewController.presentedViewController) {
        
        UIViewController* presentedViewController = rootViewController.presentedViewController;
        return [self topViewControllerWithRootViewController:presentedViewController];
    } else {
        
        return rootViewController;
    }
}
//显示
- (void)show
{
    UIWindow *shareWindow = [UIApplication sharedApplication].keyWindow;
    self.frame = CGRectMake((CGRectGetWidth(shareWindow.bounds) - kAlertWidth) * 0.5, - kAlertHeight - 30, MAIN_WIDTH-MAIN_WIDTH*0.09*2, kAlertHeight);
    [shareWindow addSubview:self];
}
//消失
- (void)dismissAlert
{
    [self removeFromSuperview];
    
}
//移除当前
- (void)removeFromSuperview
{
    [self.backImageView removeFromSuperview];
    self.backImageView = nil;
    UIWindow *shareWindow = [UIApplication sharedApplication].keyWindow;
    CGRect afterFrame = CGRectMake((CGRectGetWidth(shareWindow.bounds) - kAlertWidth) * 0.5, CGRectGetHeight(shareWindow.bounds), kAlertWidth, kAlertHeight);
    
    [UIView animateWithDuration:0.35f delay:0.0 options:UIViewAnimationOptionCurveEaseOut animations:^{
        self.frame = afterFrame;
        
        self.transform = CGAffineTransformMakeRotation(M_1_PI / 1.5);
    } completion:^(BOOL finished) {
        [super removeFromSuperview];
    }];
    
  
}

- (void)willMoveToSuperview:(UIView *)newSuperview
{
    if (newSuperview == nil) {
        return;
    }
    UIWindow *shareWindow = [UIApplication sharedApplication].keyWindow;
    
    if (!self.backImageView) {
        self.backImageView = [[UIView alloc] initWithFrame:shareWindow.bounds];
    }
    self.backImageView.backgroundColor = [UIColor blackColor];
    self.backImageView.alpha = 0.6f;
    [shareWindow addSubview:self.backImageView];
 
    self.transform = CGAffineTransformMakeRotation(-M_1_PI / 2);
    CGRect afterFrame = CGRectMake((CGRectGetWidth(shareWindow.bounds) -( MAIN_WIDTH-MAIN_WIDTH*0.09*2)) * 0.5, (CGRectGetHeight(shareWindow.bounds) - (MAIN_HEIGHT- MAIN_HEIGHT*0.15*2)) * 0.5, kAlertWidth, kAlertHeight);
    [UIView animateWithDuration:0.35f delay:0.0 options:UIViewAnimationOptionCurveEaseIn animations:^{
        self.transform = CGAffineTransformMakeRotation(0);
        self.frame = afterFrame;
    } completion:^(BOOL finished) {
    }];
    [super willMoveToSuperview:newSuperview];
}

@end
